package dk.zpon.andnoise;

import dk.zpon.andnoise.R;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.util.Linkify;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class andnoise extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		setContentView(R.layout.loginscreen);
		super.onCreate(savedInstanceState);

		Button btnhelp = (Button)findViewById(R.id.help);
		btnhelp.setOnClickListener(new OnClickListener () {
			public void onClick(View v)
			{
				showDialog(0);
			}
		});

		Button btnlogin = (Button)findViewById(R.id.login);
		// On click
		btnlogin.setOnClickListener(new OnClickListener () {
			public void onClick(View v) {
				// Get pass and ip 
				EditText txtpass = (EditText)findViewById(R.id.pass);
				EditText txtip = (EditText)findViewById(R.id.ip);

				// Get user type
				RadioButton radiouser = (RadioButton)findViewById(R.id.radio_user);
				RadioButton radioadmin = (RadioButton)findViewById(R.id.radio_admin);

				// If ip is not set
				if(txtip.getText().length() == 0)
				{
					Toast.makeText(andnoise.this, "You need to enter an IP address to log in", Toast.LENGTH_SHORT).show();
				}
				// If user type is not chosen
				else if(!radiouser.isChecked() && !radioadmin.isChecked())
				{
					Toast.makeText(andnoise.this, "You need to chose a usertype before loging in", Toast.LENGTH_SHORT).show();
				}
				// Else go ahead and connect
				else
				{
					Connection con = Connection.getInstance();
					con.setHost(txtip.getText().toString());
					con.setPort(8000);
					con.setPassword(txtpass.getText().toString());
					if(radiouser.isChecked())
						con.setUsertype(Usertype.user);
					else
						con.setUsertype(Usertype.admin);

					int ret = con.connect();
					if(ret == 1)
					{

						Intent intent = new Intent();
						Infoscreen infoscreen = new Infoscreen();
						intent.setClass(getApplicationContext(), infoscreen.getClass());

						startActivity(intent);
					}
					else if(ret == -1)
						Toast.makeText(andnoise.this, "Could not connect, check you password", Toast.LENGTH_LONG).show();
					else
						Toast.makeText(andnoise.this, "Could not connect, is your server running?", Toast.LENGTH_LONG).show();
				}

			}
		});

	}

	protected Dialog onCreateDialog(int position)
	{
		final Dialog dialog = new Dialog(this);

		dialog.setContentView(R.layout.info_dialog);
		dialog.setTitle("andNoise");

		TextView text = (TextView) dialog.findViewById(R.id.info_text);
		text.setText("Thank you for downloading andNoise\n\n" +
		"To use andNoise you need to download the andNoise server from www.code.google.com/p/andnoise, without this, you will unable asfewaf to use andnoise. At the webpage you will also find extensive documentation of how to setup and use the server and client.");
		Linkify.addLinks(text, Linkify.ALL);

		Button backbtn = (Button) dialog.findViewById(R.id.info_back);
		backbtn.setOnClickListener(new OnClickListener () {
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		return dialog;
	}
}